package com.ysd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ysd.entity.Modules;
import com.ysd.entity.Roles;
import com.ysd.repository.RolesRepository;
import com.ysd.service.RolesService;
@Service
public class RolesServiceImpl implements RolesService {
	@Autowired
	private RolesRepository rolesRepository;
	@Override
	public Page<Roles> findAllRoles(Roles r, Pageable pageable) {
		// TODO Auto-generated method stub
		return rolesRepository.findAll(get(r),pageable);
	}

	private Specification<Roles> get(Roles r) {
		// TODO Auto-generated method stub
		return new Specification<Roles>() {
			
			@Override
			public Predicate toPredicate(Root<Roles> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// TODO Auto-generated method stub
				Predicate predicate = criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				if (r.getName()!=null && r.getName()!="") {
					expressions.add(criteriaBuilder.like(root.get("Name"),"%"+r.getName()+"%" ));
					}
				return predicate;
			}
		};
	}

	@Override
	public void deleteRoles(Roles roles) {
		// TODO Auto-generated method stub
		rolesRepository.delete(roles);
	}

	@Override
	public Roles addAndUpdate(Roles s) {
		// TODO Auto-generated method stub
		return rolesRepository.saveAndFlush(s);
	}
	/*----------------------------------------------------****/

	@Override
	public List<Roles> findAllRoles() {
		// TODO Auto-generated method stub
		return rolesRepository.findAll();
	}
//通过d获取
	@Override
	public List<Roles> findRolesById(List<String> roleids) {
		// TODO Auto-generated method stub
		return rolesRepository.findAllById(roleids);
	}
	//登录权限

	@Override
	public List<Modules> functionMenu(List<String> roleids) {
		//根据角色id批量获取角色
				List<Roles> roles=rolesRepository.findAllById(roleids);
				System.out.println("Impl==>"+roles.toString());
				//根据角色里的功能集合获取功能模块并去重
				return this.getModulesByRoles(roles);
	}

	@Override
	public List<Modules> getModulesByRoles(List<Roles> roles) {
		//创建新的功能模块集合
				List<Modules> modules=new ArrayList<Modules>();
				//遍历角色集合
				for (int i = 0; i < roles.size(); i++) {
					//当前的遍历的角色
					Roles role=roles.get(i);
					//遍历该角色的功能模块集合
					for(int j=0;j<role.getModules().size();j++) {
						//当前遍历的功能模块信息
						Modules module=role.getModules().get(j);
						System.out.println("module==>"+module);
						//判断是否存在集合中(去重)
						boolean res=this.existsModule(module, modules);
						//如果不存在则添加到集合中
						if(!res) {
							modules.add(module);
						}
					}
				}
				//返回功能模块集合
				return modules;
	}

	@Override
	public boolean existsModule(Modules module, List<Modules> modules) {
		// TODO Auto-generated method stub
		return false;
	}

	public Page<Roles> findBySnamePage(Roles r,Pageable pageable) {
		// TODO Auto-generated method stub
		return rolesRepository.findAll(getc(r), pageable);
	}
	
	private  Specification<Roles> getc(Roles u){
		return new Specification<Roles>() {

			@Override
			public Predicate toPredicate(Root<Roles> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate	p=criteriaBuilder.conjunction();
				List<Expression<Boolean>>  list=p.getExpressions();
				if(!StringUtils.isEmpty(u.getName())) {
					list.add(criteriaBuilder.like(root.get("name"), "%"+u.getName()+"%"));
				}
				
				return p;
			}
		};
		
	}


	@Override
	public Roles addRoles(Roles r) {
		// TODO Auto-generated method stub
				return rolesRepository.save(r);
	}

	@Override
	public Roles updateRoles(Roles r) {
		// TODO Auto-generated method stub
				return rolesRepository.saveAndFlush(r);
	}

	@Override
	public void deleteRoles(String roleid) {
		rolesRepository.deleteById(roleid);
	}

	@Override
	public void saveUserrole(String userid, String roleid) {
		rolesRepository.saveUserrole(userid, roleid);
	}

	@Override
	public void delUserrole(String userid, String roleid) {
		rolesRepository.delUserrole(userid, roleid);
		
	}

	/**
	 * 查询某个角色信息    用于设置角色模块
	 */
	@Override
	public Optional<Roles> ByidfindModules(String Id) {
		// TODO Auto-generated method stub
		return rolesRepository.findById(Id);
	}

}
